/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.sql.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.*;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Movie;
import trefliq.EJB.mdb.NewMovieSender;
import trefliq.EJB.security.SecurityPrincipal;
import trefliq.EJB.security.SecurityRole;

/**
 *
 * @author adam
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@DeclareRoles({SecurityRole.ADMIN, SecurityRole.EMPLOYEE, SecurityRole.CUSTOMER})
@PermitAll
//@RunAs(SecurityRole.ADMIN)
public class MovieBean {
    @PersistenceContext(unitName="trefliqPU")
    private EntityManager _em;

    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public List<Movie> getAllMovies() {
        return _em.createNamedQuery("findAllMovies", Movie.class).getResultList();
    }
    
    //TODOSEC: @PermitAll
    public Movie findMovie(Long id) {
        return _em.createNamedQuery("Movie.findById", Movie.class).
                    setParameter("id", id).getSingleResult();
    }

    //TODOSEC: @PermitAll
    public Movie findMovieByTitle(String title) {
        return _em.createNamedQuery("Movie.findByTitle", Movie.class).
                    setParameter("title", title).getSingleResult();
    }

    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public boolean UpdateMovie(Movie movie) {
        _em.merge(movie);
        return true;
    }

    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public boolean DeleteMovie(Movie movie) {
        _em.remove(_em.merge(movie));
        return true;
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Long CreateMovie(String title, String desc, Long rating, Date release) {
        Movie instance = new Movie();
        instance.setTitle(title);
        instance.setRating(rating);
        instance.setDescription(desc);
        instance.setRelease(release);
        _em.persist(instance);
 
//adam - can't do this here because the call into NewMovieSender  uses an EJBContainer so it fails since previous container is still open
//        //Now we need to alert customers that want to know a new movie is in
//        NewMovieSender newMovieSender;
//        try {
//            newMovieSender = new NewMovieSender();
//            boolean result = newMovieSender.NotifyNewMovie(title);
//        } catch (NamingException ex) {
//            Logger.getLogger(MovieBean.class.getName()).log(Level.SEVERE, null, ex);
//        }  catch (JMSException ex) {
//                Logger.getLogger(MovieBean.class.getName()).log(Level.SEVERE, null, ex);
//        }

        return instance.getId();
    }
    
    //This create movie will also place a topic for a new movie being added
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Long CreateMovie(String title, String desc, Long rating, Date release, Context ec) {
        Movie instance = new Movie();
        instance.setTitle(title);
        instance.setRating(rating);
        instance.setDescription(desc);
        instance.setRelease(release);
        _em.persist(instance);
 
        //Now we need to alert customers that want to know a new movie is in
        NewMovieSender newMovieSender;
        try {
            newMovieSender = new NewMovieSender(ec);
            boolean result = newMovieSender.NotifyNewMovie(title);
        } catch (NamingException ex) {
            Logger.getLogger(MovieBean.class.getName()).log(Level.SEVERE, null, ex);
        }  catch (JMSException ex) {
                Logger.getLogger(MovieBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return instance.getId();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Movie CreateMovie(Movie movie) {
        
        _em.persist(movie);
        
        return movie;
    }
    
}
